// packageI/group_sales/my_team_sales/my_team_sales.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      titleTop: ['会员昵称','本月', '本月', '本年度'], //标题
      titleButton: ['ID','订单数', '订单总额', '订单总额'], //标题
      slideDown: false, //下拉筛选是否显示
      selectedTime: '', //当前选中的筛选时间
      currentDate: new Date().getTime(),
      minDate: new Date(2020, 0, 1).getTime(),
      currentTime:'',//默认时间 
      teamSalesList: [],

      //more
      isLoadMore: true,
      page: 1,
      total_page: 0,
      pluApiAll: {
        agentEarnings: {
          url: 'plugin.agent-earnings.frontend.sales-statistical.get-sales-detail',
          title: '团队销售额'
        },
        levelDistribution: {
          url: 'plugin.level-distribution.frontend.controllers.income.performance',
          title: '直推客户业绩'
        }
      },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let plu = options.plu||'agentEarnings';
    this.data.pluApi = this.data.pluApiAll[plu];
    wx.setNavigationBarTitle({
      title: this.data.pluApi.title
    })
    this.setData({
      currentTime:this.timestampToTime(new Date().getTime())
    })
    this.getData(this.data.currentTime);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  changeSlideDown() { //弹出关闭时间选择弹窗
    this.setData({
      slideDown:!this.data.slideDown
    })
  },

  timeToScreen(e){ //获取销售明细列表数据
    let time = e.detail;
      this.setData({
        selectedTime:this.timestampToTime(time)
      })
      this.getData(this.data.selectedTime)
      this.changeSlideDown()
  },

  cancelTimePopover(){ //时间取消修改
    this.setData({
      slideDown:false
    })
  },
  formatter(type, val) {
    if (type === 'month') {
      return `${val}月`;
    } else if (type === 'day') {
      return `${val}日`;
    }
    return val;
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },

  timestampToTime(timestamp) { //时间格式转换
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M =
      (date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1);
    return Y + M;
  },

   //获取数据
   getData(month) {
    month = parseInt(month.replace('-', ''))
    let urlStr = app.getNetAddresss(this.data.pluApi.url);
    app._postNetWork({
      url: urlStr,
      data: {month_str: month,agent_level_id: this.options.agent_level_id},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            teamSalesList: res.data.list?res.data.list.data: res.data.data
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
   //加载更多数据
   _getMoreData(){
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss(this.data.pluApi.url);
      app._postNetWork({
        url: urlStr,
        data: {
          month_str: month,
          agent_level_id: this.options.agent_level_id
        },
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let list = res.data.list?res.data.list.data: res.data.data;
            let teamSalesList = this.data.teamSalesList.concat(list);
            this.setData({teamSalesList});
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})